const basiCode = require('../util/baseCode');
/**
 * 会员
 * @param {nickName: string} 明文nick
 * @param {session: string} 
 */
exports.getMember = async (mix_nick, cloud) => {
    console.log(mix_nick,cloud) 
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.member.identity.get',
            autoSession: true,
            data: {
                'mix_nick': mix_nick,
            }
        });
        return { isOk: 1, msg: '请求成功', isMember: result.result.member_info ? true : false, data: result.result.member_info };
    } catch (err) {
        return { isOk: 0, msg: err };
    }
};
/**
 * 入会链接
 * @param {url: string} 回调链接
 */
exports.getMemberUrl = async (ctx) => {
    const { data: { url = "https://m.duanqu.com?_ariver_appid=3000000026610802&nbsv=0.0.1&nbsource=debug&nbsn=TRIAL&_mp_code=tb" }, cloud } = ctx
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.member.joinurl.get',
            autoSession: true,
            data: {
                'callback_url': url,
                'extra_info': '{\"source\":\"isvapp\",\"activityId\":\"12345677\",\"entrance\":\"hudong\"}',
            }
        });
        return basiCode(0, { isOk: 1, msg: '入会链接请求成功', data: result });
    } catch (err) {
        return await basiCode(0, { isOk: 0, msg: err });
    }
};

/**
 * 会员积分查询
 * @param {url: string} 回调链接
 */
exports.getMemberPoints = async (mix_nick, cloud) => {
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.point.available.get',
            autoSession: true,
            data: {
               'mix_nick': mix_nick,
               "buyer_nick":'zddi26'
            }
        });
        return basiCode(0, { isOk: 1, msg: '会员积分查询成功', data: result });
    } catch (err) {
        return await basiCode(0, { isOk: 0, msg: err });
    }
};
/**
 * 会员积分变更
 * @param {url: string} 回调链接
 */
exports.changePoints = async (mix_nick,cloud) => {
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.point.change',
            autoSession: true,
            data: {
                change_type:'2',  
                opt_type:'1',  
                quantity:'100',  
                mix_nick:mix_nick 
            }
        });
        return basiCode(0, { isOk: 1, msg: '会员积分更改成功', data: result });
    } catch (err) {
        return await basiCode(0, { isOk: 0, msg: err });
    }
};

